package cn.leejinki.coupons.controller;

import cn.leejinki.constant.Const;
import cn.leejinki.pojo.TbUser;
import cn.leejinki.portal.service.PortalContentService;
import cn.leejinki.utils.CookieUtils;
import cn.leejinki.utils.FastJsonConvert;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.leejinki.redis.service.JedisClient;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 京东金融 Controller
 *
 * @author ljk.
 * @create 2018-04-11
 */
@Controller
public class IndexController {

    private static final Logger logger = LoggerFactory.getLogger(IndexController.class);

    @Reference(version = Const.LJK_STORE_PORTAL_VERSION)
    private PortalContentService portalContentService;
    @Reference(version = Const.LJK_STORE_REDIS_VERSION)
    private JedisClient jedisClient;

    @Value("${redisKey.prefix.user_session}")
    private String USER_SESSION;


    @Value("${big_ad_index}")
    private long Big_AD_INDEX;


    @RequestMapping("/index")
    public String index(HttpServletRequest request, HttpServletResponse response, Model model) {

        String cookieUUID = CookieUtils.getCookieValue(request, Const.CART_KEY);
        String tokenLogin = CookieUtils.getCookieValue(request, Const.TOKEN_LOGIN);

        logger.error("cookieUUID", cookieUUID);

        TbUser user = null;
        String userJson = null;
        if (StringUtils.isNoneEmpty(tokenLogin)) {

            try {
                userJson = jedisClient.get(USER_SESSION + tokenLogin);
            } catch (Exception e) {
                logger.error("Redis 错误", e);
            }

            if (StringUtils.isNoneEmpty(userJson)) {
                user = FastJsonConvert.convertJSONToObject(userJson, TbUser.class);
            }
            model.addAttribute("user", user);
        } else {
            model.addAttribute("user", user);
        }

        model.addAttribute("author", "leejinki");//放对象
        return "index";
    }
    @RequestMapping("/index2")
    public String index2() {

        return "index";
    }
    @RequestMapping("/showError")
    public String showError() {

        return "error";
    }
    @RequestMapping("/sideBar/popupLogin")
    public String popupLogin() {

        return "popupLogin";
    }

    @RequestMapping("/adguard-ajax-api/api")
    @ResponseBody
    public String adguard() {

        return "[LString;{\"buttonPositionTop\":false,\"buttonPositionLeft\":false,\"largeIcon\":true,\"assistantFirstStart\":false,\"showWarnings\":true,\"scriptVersion\":1}";
    }
}
